package com.feng.swing.tools.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class FileChoose extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	JFileChooser chooser = null;
	JFrame f = null;
	private byte[] imageData = null;
	private boolean waiting = true;

	public synchronized byte[] getImageData() {
		while (waiting) {
			try {
				this.wait(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return imageData;
	}

	public FileChoose() throws FileNotFoundException {
		f = new JFrame();
		// f.setSize(0, 0);
		// setDefaultCloseOperation(EXIT_ON_CLOSE);
		// setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		chooser = new JFileChooser("C:\\");
		chooser.setFileFilter(new FileNameExtensionFilter("Image (*.jpg, *.gif, *.png,*.bmp)", "jpg", "png", "bmp"));
		chooser.setSize(500, 400);
		chooser.addActionListener(this);
		JButton accButton = new JButton("Accessory");
		accButton.addActionListener(this);
		f.add(accButton);
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				waiting = false;
				super.windowClosed(e);
				try {
					this.finalize();
				} catch (Throwable e1) {
					e1.printStackTrace();
				}
			}

			public void windowOpened(WindowEvent e) {
				super.windowOpened(e);
				chooser.showOpenDialog(FileChoose.this);
			}
		});
	}

	public static void main(String[] args) throws FileNotFoundException {
		FileChoose c = new FileChoose();
		c.getImageData();
		System.out.println("test");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("ApproveSelection")) {
			try {
				ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				boolean zipImg = FileChoose.zipImageFile(chooser.getSelectedFile(), outputStream, 96, 96, 1.0f);
				if (zipImg)
					imageData = outputStream.toByteArray();
				outputStream.close();
				waiting = false;
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
			System.out.println("You chose " + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName() : "nothing"));
			f.dispose();
		} else if (e.getActionCommand().equals("CancelSelection")) {
			f.dispose();
		} else if (e.getActionCommand().equals("Accessory")) {
			chooser.showOpenDialog(FileChoose.this);
		}
	}

	public static boolean zipImageFile(File file, OutputStream outputStream, int width, int height, float quality) {
		try {
			java.awt.Image srcFile = ImageIO.read(file);
			BufferedImage tag = new BufferedImage(width, height, 1);
			tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
			JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
			jep.setQuality(quality, true);
			encoder.encode(tag, jep);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
